// JavaScript Document
var indice = 0;
if(!!window.location.search) indice = parseInt(window.location.search.substring(3));

//tslideshow
(function($) {	
	$.fn.tslideshow = function(options) {
		options = $.extend({
			auto:		true,
			speed:		"slow",
			interval:	4000
		}, options);
		
		var length = this.length;
		
		return this.each(function() {
			var $this = $(this),
			$slides = $this.find("img"),
			current = 0,
			auto = options.auto,
			timer = null;
			
			function show(index) {
				var total = $slides.length;
				while(index < 0) index += total;				
				while(index >= total) index -= total;
				$slides.hide().eq(index).fadeIn(options.speed);
				current = index;
				if(auto) start();
			}
			
			function next() {
				show(current + 1);
			}
			
			function start() {
				stop();
				auto = true;
				timer = setTimeout(next, (options.interval * length) || (4000 * this.length));
			}
			
			function stop() {
				auto = false;
				clearTimeout(timer);
			}
			
			$slides.hide().eq(0).show();
			if(auto) timer = setTimeout(next, (($this.index() + 1) * options.interval));
		});
	};
})(jQuery);

//resize
$(window).on({
	resize: function() {
		$("div.scrollable").each(function() {
			$(this).data("scrollable").seekTo($(this).data("scrollable").getIndex(), 0);
		});
	}
});

//slider
$("ul.slide_tabs").tabs("div.slide", {
	effect:"fade",
	fadeOutSpeed:1000,
	fadeInSpeed:1000,
	rotate:true,
	onBeforeClick: function() {		
		var m_categ = [0, 0, 0],// 0 = madera, 1 = auto
		m_class = ["n1", "n2", "n3"],
		index = this.getIndex() + 1;
		if(!index || index >= m_categ.length) index = 0;
		$("#menu > li").removeClass("current").eq(m_categ[index]).addClass("current");
		$("#header div.top, #header div.bottom").removeClass("n1 n2 n3").addClass(m_class[index]);
	}
}).slideshow({autoplay:true, interval:3000, clickable:false});

//tabs
$("ul.tabs").tabs("div.pane", {effect:"fade"});
$("div.prod").on({
	click: function() {
		var index = ($(this).parent().index() * 5) + $(this).index();
		$("div.prod").removeClass("current");
		$(this).addClass("current");
		$("div.prod_pane").hide().eq(index).fadeIn(function() {
			var ele = $(window.location.hash);
			if(ele.length) $(window).scrollTop(ele.offset().top).scrollLeft(ele.offset().left);
		});
	}
}).eq(indice).trigger("click");
$("span.color").on({
	click: function() {
		var index = $(this).index("span.color");
		$("div.area_pane").hide().eq(index).fadeIn();
	}
}).eq(0).trigger("click");

//scrollable
$("div.scrollable").scrollable();

//lightbox
$("div.prod_info li a[rel], div.circle a[rel]").overlay({top:"center", left:"center", mask:{ color:"#000", loadSpeed:200, opacity:0.9 }});

//fade
$(".change").tslideshow();

//tooltips
$("div.color").tooltip({offset:[10, 0], position:"bottom center"});